import request from './axios';

export interface ICategoryDto {
  id: number;
  name: string;
  parentId?: number;
  level?: number;
  sort?: number;
  description?: string;
  icon?: string;
  isShow?: boolean;
  createdTime?: string;
  updatedTime?: string;
  children?: ICategoryDto[];
}

const categoryApi = {
  // 获取所有分类
  getAllCategories: () => {
    return request({
      url: 'category',
      method: 'get'
    });
  },
  
  // 获取分类树
  getCategoryTree: () => {
    return request({
      url: 'category/tree',
      method: 'get'
    });
  },
  
  // 获取根分类
  getRootCategories: () => {
    return request({
      url: 'category/root',
      method: 'get'
    });
  },
  
  // 获取子分类
  getSubcategories: (parentId: number) => {
    return request({
      url: `category/sub/${parentId}`,
      method: 'get'
    });
  },
  
  // 按层级获取分类
  getCategoriesByLevel: (level: number) => {
    return request({
      url: `category/level/${level}`,
      method: 'get'
    });
  },
  
  // 获取分类详情
  getCategory: (id: number) => {
    return request({
      url: `category/${id}`,
      method: 'get'
    });
  },
  
  // 通过名称获取分类
  getCategoryByName: (name: string) => {
    return request({
      url: `category/name/${name}`,
      method: 'get'
    });
  },
  
  // 创建分类
  createCategory: (category: ICategoryDto) => {
    return request({
      url: 'category',
      method: 'post',
      data: category
    });
  },
  
  // 更新分类
  updateCategory: (id: number, category: ICategoryDto) => {
    return request({
      url: `category/${id}`,
      method: 'put',
      data: category
    });
  },
  
  // 删除分类
  deleteCategory: (id: number) => {
    return request({
      url: `category/${id}`,
      method: 'delete'
    });
  }
};

export default categoryApi; 